package com.example.volleydemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.Random;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "hello";
    String url1 = "https://pixabay.com/get/g072aa5882e8f0bab745b12ef2b048e5b2a590ca5666773c5ff29d24fffbc71c3ae5e0f96268cd50162be39022f37a636439734d0dc7be35a656201ad9e80285a_640.jpg";
    String url2 = "https://pixabay.com/get/g83c05d8007b0aca2191d4bad505856ba499ab7c511e70fc2d4848901f0776c4d624a71f2bfc024f852236a2c6fe8ed672974fde57e3bf0c3a9cdfb0cec7b1c22_640.jpg";
    private ImageView imageView;
    private SwipeRefreshLayout swipeRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        RequestQueue queue = Volley.newRequestQueue(this);

//        final TextView textView = findViewById(R.id.textView);

        swipeRefreshLayout =  findViewById(R.id.swipeRefreshLayout);


        //swipeRefreshLayout的使用   下拉刷新界面
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {   //下拉刷新的时候调用   做什么事情
            @Override
            public void onRefresh() {
                loadImage();
            }
        });
    }

    //Glide的使用
    void loadImage() {
        Random random = new Random();
        String url = random.nextBoolean()? url1:url2;
        Glide.with(this)
                .load(url)
                .placeholder(R.drawable.ic_launcher_background)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (swipeRefreshLayout.isRefreshing()){
                            swipeRefreshLayout.setRefreshing(false);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (swipeRefreshLayout.isRefreshing()){
                            swipeRefreshLayout.setRefreshing(false);
                        }
                        return false;
                    }
                }).into(imageView);
    }
}





        //以下为Volley的使用
//        StringRequest stringRequest = new StringRequest(
//                StringRequest.Method.GET,
//                url,
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        textView.setText(response);
//                    }
//                },
//                new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
//                        Log.e(TAG, "onErrorResponse: ",error );
//                    }
//                }
//
//        );  //四个参数：请求的类型，网址，成功的回调，错误的回调
//        queue.add(stringRequest);

//        ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
//            private LruCache<String,Bitmap> cache = new LruCache<>(50/*最大缓存数量*/);  //设置缓存
//            @Override
//            public Bitmap getBitmap(String url) {
//                return cache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                cache.put(url,bitmap);
//            }
//        });

//        imageLoader.get(url, new ImageLoader.ImageListener() {
//            @Override
//            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
//                imageView.setImageBitmap(response.getBitmap());
//            }
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                Log.e(TAG, "onErrorResponse: ", error);
//            }
//        });           ImageView方法   监听器

//        imageView.setImageUrl(url,imageLoader);   //NetworkImageView方法
